package a03_哈希;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * a05_两数之和复习5
 * </p>
 *
 * @author flyduck
 * @since 2025/2/9
 */
public class a05_两数之和复习5 {
    public static void main(String[] args) {
        int[] ints = twoSum(new int[]{2, 7, 11, 15}, 9);
    }
    public static int[] twoSum(int[] nums, int target) {
        int[] result = new int[2];


        Map<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if(map.containsKey(nums[i])){
                result[0] = map.get(nums[i]);
                result[1] = i;
                return result;
            }else {
                map.put(target - nums[i],i);
            }
        }
        return result;


    }
}
